package com.etc.controller;

import com.etc.pojo.User;
import com.etc.pojo.Video;
import com.etc.service.UserPersonageService;
import com.etc.service.VideoPersonageService;
import com.etc.util.PasswordUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.awt.ModalExclude;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.logging.Logger;

@Controller
@RequestMapping("UserPersonageController")
public class UserPersonageController {

    @Autowired
    UserPersonageService userPersonageService;

    @ResponseBody
    @RequestMapping(value = "updateUserImg")
    public boolean updateUserImg(@RequestParam("file") MultipartFile multipartFile,
                              HttpSession session) {
        String filename = multipartFile.getOriginalFilename();
        String imgPath = UUID.randomUUID().toString() + filename;
        File file = new File("e:\\exercisefigure\\" + imgPath);
        User user = (User)session.getAttribute("user");
        try {
            multipartFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }


        boolean leep = userPersonageService.updateUserImg(user, imgPath);
        User user1 = userPersonageService.queryUserByUserId(user);
        session.setAttribute("user",user1);
        return leep;
    }

    @ResponseBody
    @RequestMapping(value = "updateUserPassword")
    public boolean updateUserPassword(@RequestParam("password") String password,
                                      @RequestParam("newPassword") String newPassword,
                                 HttpSession session) {
        User user = (User)session.getAttribute("user");
        User users = userPersonageService.queryUserByUserId(user);
        String passs = PasswordUtil.md5(password + users.getSalt());
        String pass = PasswordUtil.md5(newPassword + users.getSalt());
        if(passs.equals(user.getPassword())) {
            boolean b = userPersonageService.updateUserPassword(user, pass);
            return b;
        } else {
            return false;
        }
    }

    @ResponseBody
    @RequestMapping(value = "exitUser")
    public boolean exitUser(HttpSession session) {
        session.removeAttribute("user");
        return true;
    }

    @ResponseBody
    @RequestMapping(value = "pass")
    public boolean pass(HttpSession session,@Param("time") String time) {
        session.setAttribute("time",time);
        return true;
    }


    @RequestMapping(value = "updateUserVip")
    public ModelAndView updateUserVip(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView();
        User user = (User)session.getAttribute("user");
        String time = (String)session.getAttribute("time");
        int times = Integer.parseInt(time);
        if (user.getVipState() == 1) {
            boolean leep = userPersonageService.updateUserVipTime(user,times);
            User user2 = userPersonageService.queryUserByUserId(user);
            session.setAttribute("user",user);

        } else if (user.getVipState() == 0) {
            boolean leep = userPersonageService.addUserVip(user, times);
            boolean leep1 = userPersonageService.updateUserVipState(user);
            User user2 = userPersonageService.queryUserByUserId(user);
            session.setAttribute("user",user2);
;
        }
        modelAndView.setViewName("forward:/front/CenTer.jsp");
        return modelAndView;
    }


}
